#include "gameapplication.h"

#include <QTextCodec>
#include <QSettings>
#include <QMessageBox>

#include <handgestureexception.h>

GameApplication::GameApplication(int &argc, char **argv) :
	QApplication(argc, argv) {
	QTextCodec::setCodecForTr(QTextCodec::codecForName ("UTF-8"));
	QCoreApplication::setOrganizationName(QString("MinixL"));
	QCoreApplication::setApplicationName(QString("handplane"));
	QCoreApplication::setApplicationVersion(QString("1.0"));

	loadSettings();
}

GameApplication* GameApplication::instance() {
	return (static_cast<GameApplication *>(QCoreApplication::instance()));
}

void GameApplication::loadSettings() {

	QSettings settings;

	settings.beginGroup(QString("camera"));
	camIdSettings_ = settings.value("camId", "-1").toInt();
	settings.endGroup();
	settings.beginGroup(QString("skinThreshold"));
	lowSkinThresholdSettings_ = settings.value("lowHue", "-1").toInt();
	upperSkinThresholdSettings_ = settings.value("upperHue", "-1").toInt();
	lowValueThresholdSettings_ = settings.value("lowValue", "-1").toInt();
	upperValueThresholdSettings_ = settings.value("upperValue", "-1").toInt();
	adaptThresholdSettings_ = settings.value("adaptThresholding", 1).toInt() ? true : false;
	settings.endGroup();
	settings.beginGroup(QString("tracking"));
	trackingSensity_ = settings.value("sensity", "0.9").toDouble();
	settings.endGroup();
	settings.beginGroup(QString("additionaWindows"));
	showImageSettings_ = settings.value("showImage", 1).toBool();
	showMaskSettings_ = settings.value("showMask", 1).toBool();
	settings.endGroup();
}

int GameApplication::camIdSettings_ = -1;
int GameApplication::lowSkinThresholdSettings_ = -1;
int GameApplication::upperSkinThresholdSettings_ = -1;
double GameApplication::trackingSensity_ = -1;
int GameApplication::lowValueThresholdSettings_ = -1;
int GameApplication::upperValueThresholdSettings_ = -1;
bool GameApplication::adaptThresholdSettings_ = true;
bool GameApplication::showImageSettings_ = true;
bool GameApplication::showMaskSettings_ = true;

int GameApplication::getCamIdSettings() {
	return camIdSettings_;
}

void GameApplication::getSkinThresholds(int &low, int &upper) {
	low = lowSkinThresholdSettings_;
	upper = upperSkinThresholdSettings_;
}

void GameApplication::getValueThreshold(int &low, int &upper) {
	low = lowValueThresholdSettings_;
	upper = upperValueThresholdSettings_;
}

double GameApplication::getTrackingSensity() {
	return trackingSensity_;
}

bool GameApplication::getAdaptThreshold() {
	return adaptThresholdSettings_;
}

bool GameApplication::getShowImageSettings() {
	return showImageSettings_;
}

bool GameApplication::getShowMaskSettings() {
	return showMaskSettings_;
}

bool GameApplication::notify( QObject *o, QEvent *e ) {
	 try {
		return QApplication::notify(o, e);
	}
	catch (mlgesture::HandGestureException &e) {
		QMessageBox::critical(NULL, tr("Błąd"), e.what());
		quit();
	}
	return false;
}
